package com.lcj.ems.service.impl;

import com.lcj.ems.dao.EmpDao;
import com.lcj.ems.dao.impl.EmpDaoImpl;
import com.lcj.ems.entity.Emp;
import com.lcj.ems.exception.DataAccessException;
import com.lcj.ems.exception.RequestParameterException;
import com.lcj.ems.exception.ServiceException;
import com.lcj.ems.service.EmpService;
import com.lcj.ems.util.CommonUtils;

import java.util.List;

public class EmpServiceImpl implements EmpService {
    EmpDao empDao = new EmpDaoImpl();

    @Override
    public void addEmp(String name, String salary, String gender, String edu, String[] hobbies) throws RequestParameterException, ServiceException {
        if(CommonUtils.isNull(name)||CommonUtils.isNull(salary)||CommonUtils.isNull(gender)||CommonUtils.isNull(edu))
            throw new RequestParameterException("参数格式异常");
       Emp emp = new Emp(name,Double.parseDouble(salary),gender,edu,hobbies);
        try {
            empDao.insertEmp(emp);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }catch (NumberFormatException e)
        {
            throw new RequestParameterException("数据格式有误");
        }
    }

    @Override
    public List<Emp> getAll() throws ServiceException {
        try {
            List<Emp> list = empDao.selectAll();
            return list;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void modify(String id, String name, String salary, String gender, String edu, String[] hobbies) throws RequestParameterException, ServiceException {
        if(CommonUtils.isNull(id)||CommonUtils.isNull(name)||CommonUtils.isNull(salary)||CommonUtils.isNull(gender)||CommonUtils.isNull(edu))
            throw new RequestParameterException("参数格式异常");
        Emp emp = new Emp(name,Double.parseDouble(salary),gender,edu,hobbies);
        emp.setId(Integer.parseInt(id));
        try {
            empDao.updateEmp(emp);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }catch (NumberFormatException e)
        {
            throw new RequestParameterException("数据格式有误");
        }
    }

    @Override
    public Emp getEmpById(String id) throws RequestParameterException, ServiceException {
        if(CommonUtils.isNull(id))
            throw new RequestParameterException("编号获取异常");
        Integer i = Integer.parseInt(id);
        Emp emp = null;
        try {
            emp = empDao.selectEmpById(i);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
        return emp;
    }

    @Override
    public void removeEmp(String id) throws RequestParameterException, ServiceException {
        if(CommonUtils.isNull(id))
            throw new RequestParameterException("编号获取异常");
        try {
            empDao.deleteEmpById(Integer.parseInt(id));
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }
}
